# 获取所有频道视图函数
from flask_restful import Resource
from sqlalchemy.orm import load_only

from models.article import Channel


class AllChannelResource(Resource):
    """获取所有频道"""

    def get(self):
        # 查询所有频道数据
        channels = Channel.query.options(load_only(Channel.id, Channel.name)).all()

        # 序列化
        channel_list = [channel.to_dict() for channel in channels]

        # 返回数据
        return {'channels': channel_list}
